﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;
#if 0
class Time
{
public:
	Time(int hour = 0)
		:_hour(hour)
	{
		cout << "Time()" << endl;
	}
private:
	int _hour;
};


class Date
{
public:
	//初始化列表
	Date(int& rx, int year, int month, int day)
		//成员变量定义
		:_year(year)
		, _month(month)
		, _day(day)
		, _n(1)
		, _ref(rx)

		//如果没有合适的默认构造函数
		, _t(1)

		, _ptr((int*)malloc(12))

	{
		//error
		//_n = 1;
		if (_ptr == nullptr)
		{
			perror("malloc operation failed");
			return;
		}
		else
		{
			memset(_ptr, 0, 12);
		}
	};

	void Print() const
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	//C++11，声明，缺省值->初始化列表用的
	int _year = 1;
	int _month = 1;
	int _day = 1;

	//const常量成员只能在初始化列表中初始化
	const int _n;

	//引用成员只能在初始化列表中初始化
	int& _ref;

	//会调用Time _t的默认构造函数
	Time _t = 1;

	int* _ptr = (int*)malloc(12);
};

int main()
{
	int x = 0;
	//对象定义
	Date d1(x, 2024, 7, 16); 

	//const变量只能在定义的时候初始化一次
	//const int x = 1;
	//x = 1;

	return 0;
}
#endif

#if 0
#include<iostream>
using namespace std;

class A
{
public:
	A(int a)
		: _a1(a)
		, _a2(_a1)
	{}
	
	void Print() 
	{
		cout << _a1 << " " << _a2 << endl;
	}
private:
	int _a2 = 2;
	int _a1 = 2;
};

int main()
{
	A aa(1);
	aa.Print();//输出1 随机值
}
#endif

#if 0
class A
{
public:
	//explicit A(int a = 0) 不支持隐式类型转换
	 A(int a = 0)
	{
		_a1 = a;
	}

	A(const A& aa)
	{
		_a1 = aa._a1;
	}

	A(int a1, int a2)
		 :_a1(a1)
		, _a2(a2)
		{}

	void Print()
	{
		cout << _a1 << " " << _a2 << endl;
	}
private:
	int _a1;
	int _a2;
};

class Stack
{
public:
	void Push(const A& aa)
	{
		//...
	}
private:
	A _arr[10];
	int _top;
};

int main()
{
	A aa1(1);
	aa1.Print();

	//隐式类型转换
	//2构造一个A的临时对象，在用这个临时对象拷贝构造aa2
	//编译器遇到连续构造+拷贝构造->优化为直接构造
	A aa2 = 2;
	aa2.Print();

	A& raa1 = aa2;
	const A& raa2 = 2;

	//类型转换的意义
	Stack st;
	A aa3(3);
	st.Push(aa3);

	st.Push(3);

	//C++11多参数情况
	A aa5 = { 1, 1 };
	const A& raa6 = { 2,2 };
	st.Push(aa5);
	st.Push({ 2,2 });

	return 0;
}
#endif

#if 0
#include <iostream>
using namespace std;

class A
{
public:
	A()
	{
		++_scount;
	}
	A(const A& t)
	{
		++_scount;
	}	

	~A()
	{
		 --_scount;
	}
	//静态成员函数
	static int GetACount()
	{
		//++_a;
		//没有this指针，不能访问非静态成员变量	this->_a
		return _scount;
	}
//private:
	//类里面声明
	static int _scount;
	int _a;
};

//类外面初始化
int A::_scount = 0;

int main()
{
	//大小为1，不包含静态变量，它存在静态区
	cout << sizeof(A) << endl;

	//公有情况下，可以用A::_scount或a1_.scount
	cout << A::_scount << endl;

	//否则用静态成员函数获取
	cout << A::GetACount() << endl;
	A a1, a2;
	{
		A a3(a1);
		cout << A::GetACount() << endl;
	}
	cout << A::GetACount() << endl;
	cout << a1.GetACount() << endl;

	return 0;
}
#endif


//友元
#if 0
#include <iostream>
using namespace std;

//前置声明，否则A的友元函数声明编译器不认识B
class B;

class A
{
	//友元声明
	friend void function(const A& aa, const B& bb);
	//类型变量向上找，找不到B
private:
	int _a1 = 1;
	int _a2 = 2;
};
class B
{
	//友元声明
	friend void function(const A& aa, const B& bb);
private:
	int _b1 = 3;
	int _b2 = 4;
};

void function(const A& aa, const B& bb)
{
	cout << aa._a1 << endl;
	cout << bb._b1 << endl;
}

int main()
{
	A aa;
	B bb;
	function(aa, bb);

	return 0;
}
#endif

#if 0
#include <iostream>
using namespace std;

class A
{
	//友元类
	friend class B;
private:
	int _a1 = 1;
	int _a2 = 2;
};
class B
{
public:
	void Func1(const A& aa)
	{
		cout << aa._a1 << endl;
		cout << _b1 << endl;
	}
	void Func2(const A& aa)
	{
		cout << aa._a2 << endl;
		cout << _b2 << endl;
	}
private:
	int _b1 = 3;
	int _b2 = 4;
};

int main()
{
	A aa;
	B bb;
	bb.Func1(aa);
	bb.Func2(aa);

	return 0;
}
#endif


#if 0
#include <iostream>
using namespace std;

class A
{
private:
	static int _k;
	int _h = 1;
public:
	class B//B默认就是A的友元
	{
	public:
		void foo(const A& a)
		{
			cout << _k << endl;//OK
			cout << a._h << endl;//OK
		}
	private:
		int _b = 1;
	};
};

int A::_k = 1;

int main()
{
	cout << sizeof(A) << endl;
	A::B b;
	A aa;
	b.foo(aa);

	return 0;
}
#endif

#if 0
#include <iostream>
using namespace std;

class A
{
public:
	A(int a = 0)
		: _a(a)
	{
		cout << "A(int a)" << endl;
	}
	~A()
	{
		cout << "~A()" << endl;
	}
private:
	int _a;
};

class Solution {
public:
	int Sum_Solution(int n) {
		//...
		return n;
	}
};

int main()
{
	//有名对象
	 A aa1;

	//匿名对象
	A(1);

	//有名对象调用
	Solution st;
	int ret1 = st.Sum_Solution(10);
	cout << ret1 << endl;
	
	//匿名对象在这样场景下就很好用
	int ret2 = Solution().Sum_Solution(10);
	cout << ret2 << endl;

	return 0;
}
#endif 

#if 0
#include <iostream>
using namespace std;
#include <algorithm>

int main()
{
	int arr[] = { 32,70,12,45,26,80,53,33 };
	sort(arr, arr + 8);
	for (int i = 0; i < 8; i++)
	{
		cout << arr[i] << " ";
	}

	return 0;
}
#endif



#include <iostream>
using namespace std;

class A
{
public:
	A(int a = 0)
		: _a1(a)
	{
	cout << "A(int a)" << endl;
	}
	A(const A& aa)
		:_a1(aa._a1)
	{
		cout << "A(const A& aa)" << endl;
	}
	
	A& operator=(const A & aa)
	{
		cout << "A& operator=(const A& aa)" << endl;
		
		if (this != &aa)
		{
			_a1 = aa._a1;
		}
		
		return *this;
	}
	
	~A()
	{
		cout << "~A()" << endl;
	}

	void Print()
	{
		cout << "A::Print->" << _a1 << endl;
	}
	A& operator++()
	{
		++_a1;
		return *this;
	}
private:
	int _a1 = 1;
};

#if 0
int main()
{
	//构造+拷贝构造->优化为直接构造
	A aa1 = 1;
	//若const引用，直接构造临时对象，然后引用该临时对象
	const A& aa2 = 1;

	return 0;
}
#endif

#if 0
void f1(A aa)
{
	//...
}

int main()
{
	f1(A(1));//构造+拷贝构造
	cout << endl;

	f1(1);//构造+拷贝构造
	cout << endl;

	return 0;
}
#endif

#if 0
A f2()
{
	A aa(1);
	++aa;
	return aa;
}

int main()
{
	f2().Print();
	cout << endl;

	return 0;
}
#endif


A f2()
{
	A aa(1);
	return aa;
}

int main()
{
	A ret = f2();
	ret.Print();
	cout << "xxxxx" << endl;

	return 0;
}